package com.nine.news.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.nine.news.mapper.ConfigMapper;

import javax.annotation.Resource;

/**
 * @Author: szhan
 * @Date: 2023/03/16/8:10 PM
 * @Description:
 */
public class SmsUtil {

    @Resource
    private ConfigMapper mapper;

    private static  final String KEY;
    private static  final String SECRET;
    /**
     * 注册验证码模板*/
    public static  final String REGISTER;
    /**
     * 登录验证码模板*/
    public static final String LOGIN;
    /**
     * 密码找回验证码模板*/
    public static  final String FIND;
    private static Client client;

     static {
//        KEY = "LTAI5tM1kdcVRgR73UjpETxM";
//        SECRET="mYxdY9kC5XCXCi2CTOp6qpU4nfuh8v";
//        REGISTER="SMS_114390520";
//        LOGIN="SMS_115250125";
//        FIND="SMS_177258097";
        KEY = "LTAI5tCu3ZU5jq6B6Z2A9TUd";
        SECRET="9b1BI7eALGHTDs3U8aJO6jumOuvZDi";
        REGISTER="SMS_274610936";
        LOGIN="SMS_274610936";
        FIND="SMS_274610936";
        //KEY = mapper.search("sms_key");
        Config config = new Config()
                .setAccessKeyId(KEY)
                .setAccessKeySecret(SECRET);
        try {
            client=new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean sendMsg(String tem,String phone,int code){
        //1.实例化 SMS请求对象
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phone)
                //.setSignName("来自邢朋辉的短信")
                .setSignName("知至止行")
                .setTemplateCode(tem)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        //2.发送短信 获取响应对象
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            SendSmsResponse response=client.sendSmsWithOptions(request, runtime);
            return response.getBody().getCode().equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
